package top.starmc.starpet.execute

import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import taboolib.common.platform.command.*
import taboolib.expansion.createHelper
import taboolib.platform.util.giveItem
import top.starmc.starpet.config.Config
import top.starmc.starpet.util.getPlayerData

@CommandHeader(name = "StarPet", permission = "StarPet.Default")
object ExecuteHandler {
    @CommandBody
    val main = mainCommand {
        createHelper()
    }

    @CommandBody(permission = "StarPet.Give")
    val give = subCommand {
        dynamic("宠物") {
            suggestionUncheck<CommandSender> { _, _ ->
                Config.internalMap.keys.toList()
            }
            player("玩家") {
                execute<CommandSender> { sender, context, _ ->
                    Config.internalMap[context["宠物"]]?.let {
                        context.player("玩家").castSafely<Player>()?.giveItem(it.internalItem)
                        sender.sendMessage("§f[§c系统§f] §c宠物已发送到玩家背包")
                    } ?: let {
                        sender.sendMessage("§f[§c系统§f] §c宠物不存在")
                    }
                }
            }
            execute<Player> { sender, context, _ ->
                Config.internalMap[context["宠物"]]?.let {
                    sender.giveItem(it.internalItem)
                    sender.sendMessage("§f[§c系统§f] §c宠物已发送到你的背包")
                } ?: let {
                    sender.sendMessage("§f[§c系统§f] §c宠物不存在")
                }
            }
        }
    }

    @CommandBody(permission = "StarPet.AddExp")
    val addExp = subCommand {
        int("经验") {
            player("玩家") {
                execute<CommandSender> { sender, context, _ ->
                    context.player("玩家").castSafely<Player>()?.let {
                        with(it.getPlayerData()) {
                            if (internal != null) {
                                addExp(context.int("经验"))
                            } else {
                                sender.sendMessage("§f[§c系统§f] §c玩家没有装备宠物")
                            }
                        }
                    }
                }
            }
            execute<Player> { sender, context, _ ->
                with(sender.getPlayerData()) {
                    if (internal != null) {
                        addExp(context.int("经验"))
                        sender.sendMessage("§f[§c系统§f] §c为玩家宠物增加了经验")
                    } else {
                        sender.sendMessage("§f[§c系统§f] §c你没有装备宠物")
                    }
                }
            }
        }
    }

    @CommandBody(permission = "StarPet.Reload")
    val reload = subCommand {
        execute<CommandSender> { sender, _, _ ->
            Config.loadConfig()
            Bukkit.getOnlinePlayers().forEach { it ->
                with(it.getPlayerData()) {
                    item?.let {
                        setPet(it)
                    }
                }
            }
            sender.sendMessage("§f[§c系统§f] §c配置已重载")
        }
    }
}